
package com.carsite.webapp.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;

import com.carsite.Constants;
import com.carsite.model.Department;
import com.carsite.model.Material;
import com.carsite.service.DepartmentManager;
import com.carsite.service.MaterialManager;
import com.carsite.webapp.form.MaterialForm;

/**
 * Action class to handle CRUD on a Material object
 *
 * @struts.action name="materialForm" path="/materials" scope="request"
 *  validate="false" parameter="method" input="mainMenu"
 * @struts.action name="materialForm" path="/editMaterial" scope="request"
 *  validate="false" parameter="method" input="list"
 * @struts.action name="materialForm" path="/saveMaterial" scope="request"
 *  validate="true" parameter="method" input="edit"
 * @struts.action-set-property property="cancellable" value="true"
 * @struts.action-forward name="edit" path="/WEB-INF/pages/materialForm.jsp"
 * @struts.action-forward name="list" path="/WEB-INF/pages/materialList.jsp"
 * @struts.action-forward name="search" path="/materials.html" redirect="true"
 */
public final class MaterialAction extends BaseAction {
    public ActionForward cancel(ActionMapping mapping, ActionForm form,
                                HttpServletRequest request,
                                HttpServletResponse response)
    throws Exception {
        return mapping.findForward("search");
    }

    public ActionForward delete(ActionMapping mapping, ActionForm form,
                                HttpServletRequest request,
                                HttpServletResponse response)
    throws Exception {
        if (log.isDebugEnabled()) {
            log.debug("Entering 'delete' method");
        }

        ActionMessages messages = new ActionMessages();
        MaterialForm materialForm = (MaterialForm) form;

        // Exceptions are caught by ActionExceptionHandler
        MaterialManager mgr = (MaterialManager) getBean("materialManager");
        mgr.removeMaterial(materialForm.getId());

        messages.add(ActionMessages.GLOBAL_MESSAGE,
                     new ActionMessage("material.deleted"));

        // save messages in session, so they'll survive the redirect
        saveMessages(request.getSession(), messages);

        return mapping.findForward("search");
    }

    public ActionForward edit(ActionMapping mapping, ActionForm form,
                              HttpServletRequest request,
                              HttpServletResponse response)
    throws Exception {
        if (log.isDebugEnabled()) {
            log.debug("Entering 'edit' method");
        }

        MaterialForm materialForm = (MaterialForm) form;

        // if an id is passed in, look up the user - otherwise
        // don't do anything - user is doing an add
        if (materialForm.getId() != null) {
            MaterialManager mgr = (MaterialManager) getBean("materialManager");
            Material material = mgr.getMaterial(materialForm.getId());
            materialForm = (MaterialForm) convert(material);
            updateFormBean(mapping, request, materialForm);
        }

        return mapping.findForward("edit");
    }

    public ActionForward save(ActionMapping mapping, ActionForm form,
                              HttpServletRequest request,
                              HttpServletResponse response)
    throws Exception {
        if (log.isDebugEnabled()) {
            log.debug("Entering 'save' method");
        }

        // Extract attributes and parameters we will need
        ActionMessages messages = new ActionMessages();
        MaterialForm materialForm = (MaterialForm) form;
        boolean isNew = ("".equals(materialForm.getId()) || materialForm.getId() == null);

        MaterialManager mgr = (MaterialManager) getBean("materialManager");
        Material material = (Material) convert(materialForm);
        mgr.saveMaterial(material);

        // add success messages
        if (isNew) {
            messages.add(ActionMessages.GLOBAL_MESSAGE,
                         new ActionMessage("material.added"));

            // save messages in session to survive a redirect
            saveMessages(request.getSession(), messages);

            return mapping.findForward("search");
        } else {
            messages.add(ActionMessages.GLOBAL_MESSAGE,
                         new ActionMessage("material.updated"));
            saveMessages(request, messages);

            return mapping.findForward("edit");
        }
    }

    public ActionForward search(ActionMapping mapping, ActionForm form,
                                HttpServletRequest request,
                                HttpServletResponse response)
    throws Exception {
        if (log.isDebugEnabled()) {
            log.debug("Entering 'search' method");
        }

        MaterialForm materialForm = (MaterialForm) form;
        Material material = (Material) convert(materialForm);
        
        DepartmentManager deptMmgr = (DepartmentManager) getBean("departmentManager");
        List<Department> list=deptMmgr.getWarehouse();
        request.getSession().setAttribute(Constants.DEPARTMENT_LIST, list);
        
        MaterialManager mgr = (MaterialManager) getBean("materialManager");
        request.setAttribute(Constants.MATERIAL_LIST, mgr.getMaterials(material));
        
        return mapping.findForward("list");
    }
    public ActionForward unspecified(ActionMapping mapping, ActionForm form,
                                     HttpServletRequest request,
                                     HttpServletResponse response)
            throws Exception {
        return search(mapping, form, request, response);
    }
}
